#!/bin/bash
# File Name: backup.sh
# Author: moshan
# mail: mo_shan@yeah.net
# Created Time: 2018-10-15 17:49:27
# Function: 
#########################################################################
#git clone ssh://git@ssh.gitlab.mfwdev.com:333/WebDev/outdated.git && cd outdated && git branch  moshan 
#git clone ssh://git@ssh.gitlab.mfwdev.com:333/WebDev/rundata.git && cd rundata && git branch  moshan
#记得在git上面删除个人分支
project_name="mgr_worldcup2018"
branch_name="moshan"
mysql_port=3308
mysql_cnf="/mfw_data/mysql_${mysql_port}/my_${mysql_port}.cnf"
project_path="/Users/moshan"

function add_backup()
{
	project_path="${project_path}/outdated"
	cd ${project_path}
	git checkout master
	git pull
	git checkout ${branch_name}
	git merge master
	cd www2011/include/dataman/backup/
	row="$(grep -n "/\* mysql backup \*/" dataNodeConf.php|awk -F: '{print $1}')"
	sed -n "1,${row}p" dataNodeConf.php > tmp.log
	echo -e "        '${project_name},backup' => array(\n        'hour' => 1, 'minute' => 2, 'fg' => true, 'path' => '/mfw_www/app/scripts/mysql',\n        'cmd' => 'php db_backup.php ${mysql_cnf} ${project_name} ${mysql_port}',\n        )," >> tmp.log
	sed -n "$((${row}+1)),\$p" dataNodeConf.php >> tmp.log
	cat dataNodeConf.php > dataNodeConf_back.php
	cat  tmp.log > dataNodeConf.php
	head -15 dataNodeConf.php dataNodeConf_back.php
	read -p "请检查备份任务添加的是否正确，无误后输入y继续" choose
	if [ "${choose}x" != "yx" ]
	then
		cat dataNodeConf_back.php > dataNodeConf.php
		exit
	fi
	git add dataNodeConf.php
	git commit -m "add new backup for ${project_name}_1"
	git push --set-upstream origin ${branch_name}
}

function add_file()
{
	project_path="${project_path}/rundata"
	cd ${project_path}
	git checkout master
	git pull
	git checkout ${branch_name}
	git merge master
	cd backup/db/mysql/
	mkdir ${project_name}
	touch ${project_name}/readme.md
	git add ${project_name}/readme.md
	git commit -m "add new backup for ${project_name}_2"
	git push --set-upstream origin ${branch_name}
}

function add_backup_con()
{
	project_path="${project_path}/outdated"
	cd ${project_path}
	git checkout master
	git pull
	git checkout ${branch_name}
	git merge master
	cd www2011/app/stat/server_stat/monitor/
	row="$(grep -n "\$aRole = array(" check_mysql.php|awk -F: '{print $1}')"
	sed -n "1,${row}p" check_mysql.php > tmp.log
	echo -e "   '${project_name}'," >> tmp.log
	sed -n "$((${row}+1)),\$p" check_mysql.php >> tmp.log
	cat check_mysql.php > check_mysql_back.php
	cat  tmp.log > check_mysql.php
	head -15 check_mysql.php check_mysql_back.php
	read -p "请检查备份任务添加的是否正确，无误后输入y继续" choose
	if [ "${choose}x" != "yx" ]
	then
		cat check_mysql_back.php > check_mysql.php
		exit
	fi
	#编辑好再往下
	git add check_mysql.php
	git commit -m "add new backup for ${project_name}_3"
	git push --set-upstream origin ${branch_name}
}
case $1 in
	1)
		add_backup
		;;
	2)
		add_file
		;;
	3)
		add_backup_con
		echo "等待流程审批完成以后，在aos系统发布，发布成功以后到备份节点验证备份是否成功"
		echo "cd /mfw_project/www2011/app/scripts/mysql/ && php db_backup.php ${mysql_cnf} ${project_name} ${mysql_port}"
		;;
esac

